﻿@model string
@inject IPermissionService permissionService
@inject AdminAreaSettings adminAreaSettings
@using Nop.Web.Areas.Admin.Helpers
@using Nop.Core
@using Nop.Services.Security
@using Nop.Web.Framework.UI
@{
    //we do not bundle this script file (does not work for some reasons in bundle)
    Html.AddScriptParts("~/lib/tinymce/tinymce.min.js", excludeFromBundle: true);

    // tinyMCE language
    var language = TinyMceHelper.GetTinyMceLanguage();

    var allowRoxyFileman = permissionService.Authorize(StandardPermissionProvider.HtmlEditorManagePictures);

    var random = CommonHelper.GenerateRandomInteger();

    //extend editor with additional settings
    //Sample setting value (below):
    //settings.plugins.push('print'); settings.toolbar += ' | print';
    var additionalEditorSettings = adminAreaSettings.RichEditorAdditionalSettings;
    //is java-script supported?
    var allowJavaScript = adminAreaSettings.RichEditorAllowJavaScript;

    //allow HTML body? Full page? - http://www.tinymce.com/wiki.php/Plugin:fullpage
    //false by default
    var enableFullPage = Convert.ToBoolean(ViewData["nop.RichEditor.EnableFullPage"] ?? false);

    var extended_valid_elements = new List<string>();
    var valid_children = new List<string>();
    
    if(allowJavaScript)
    {
        extended_valid_elements.Add("script[charset|defer|language|src|type]");
        valid_children.Add("+body[script]");
    }

    if (adminAreaSettings.RichEditorAllowStyleTag)
    {
        extended_valid_elements.Add("style[dir<ltr?rtl|lang|media|title|type]");
        extended_valid_elements.Add("link[dir<ltr?rtl|href|hreflang|lang|media|rel|rev|title|target|type]");

        valid_children.Add("+body[style]");
        valid_children.Add("+body[link]");
    }
}

<script>
    $(document).ready(function() {
        @* wooncherk contribution *@
        function RoxyFileBrowser@(random)(field_name, url, type, win) {
            //we manually generate the configuration file to ensure that it works fine in virtual directory
            $.ajax({
                cache: false,
                type: "GET",
                url: "@(Url.Action("CreateConfiguration", "RoxyFileman"))",
                success: function (data, textStatus, jqXHR) {
                    var roxyFileman = '@Url.Content("~/lib/Roxy_Fileman/index.html")';
                    if (roxyFileman.indexOf("?") < 0) {
                        roxyFileman += "?type=" + type;
                    } else {
                        roxyFileman += "&type=" + type;
                    }
                    roxyFileman += '&input=' + field_name + '&value=' + document.getElementById(field_name).value;
                    tinyMCE.activeEditor.windowManager.open({
                        file: roxyFileman,
                        title: 'Roxy Fileman',
                        width: 850,
                        height: 650,
                        resizable: "yes",
                        plugins: "media",
                        inline: "yes",
                        close_previous: "no"
                    }, { window: win, input: field_name });
                    return false;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#createConfigurationFailed-info').text(errorThrown);
                    $("#createConfigurationFailed").click();
                }
            });
        }

        var defaultEditorSettings@(random) = {
            selector: "#@Html.IdForModel()",
            fontsize_formats: "8pt 9pt 10pt 11pt 12pt 26pt 36pt",
            height: 200,
            width: '100%',
            autoresize_min_height: 200,
            autoresize_max_height: 200,
            plugins: [
                "advlist autolink autoresize directionality lists link image charmap preview anchor",
                "searchreplace visualblocks code fullscreen textcolor",
                "insertdatetime media table contextmenu paste@(enableFullPage ? " fullpage" : null)"
            ],
            toolbar: "ltr rtl | insertfile undo redo | styleselect | fontselect | fontsizeselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
            relative_urls: false,
            @if (allowRoxyFileman)
            {
                <text>
                    //picture manager
                    file_browser_callback: RoxyFileBrowser@(random),
                </text>
            }
            @if (!string.IsNullOrEmpty(language))
            {
                <text>
                    //language
                    language: "@language",
                </text>
            }
            //we should set "convert_urls" set to "false" (because of "relative_urls" requirement)
            //otherwise, it'll break existing links when editing message template
            convert_urls: false,
            //ensure that special chars are not encoded. For example, ä, ö and ü should NOT become "&auml;", "&ouml;" and "&uuml;"
            //Otherwise, database searches become impossible when words contain a character of this kind
            entity_encoding: "raw",
            @if (allowJavaScript)
            {
                <text>
                    allow_script_urls: true,
                </text>
            }
            else
            {
                <text>
                    invalid_elements: "script",
                </text>
            }
            @if (extended_valid_elements.Any())
            {
                <text>
                    extended_valid_elements: "@Html.Raw(string.Join(',', extended_valid_elements))",
                </text>
            }
            @if (valid_children.Any())
            {
                <text>
                    valid_children: "@Html.Raw(string.Join(',', valid_children))"
                </text>
            }
        };

        function getAdditionalEditorSettings@(random)(settings) {
            @Html.Raw(additionalEditorSettings)
            return settings;
        }

        tinyMCE.init(getAdditionalEditorSettings@(random)(defaultEditorSettings@(random)));
    });
</script>

<textarea asp-for="@Model">@ViewData.TemplateInfo.FormattedModelValue</textarea>
<nop-alert asp-alert-id="createConfigurationFailed" />
